<template>
  <div class="login-container">
    <div class="login-box">
      <div class="login-header">
        <img src="@/assets/logo.png" alt="Logo" class="logo" />
        <h1 class="title">BDMobile OPS</h1>
        <p class="subtitle">运维管理平台</p>
      </div>

      <div class="login-form">
        <van-form @submit="onSubmit">
          <van-cell-group inset>
            <van-field
              v-model="username"
              name="username"
              placeholder="请输入用户名"
              :rules="[{ required: true, message: '请输入用户名' }]"
            >
              <template #left-icon>
                <van-icon name="user-o" />
              </template>
            </van-field>
            <van-field
              v-model="password"
              type="password"
              name="password"
              placeholder="请输入密码"
              :rules="[{ required: true, message: '请输入密码' }]"
            >
              <template #left-icon>
                <van-icon name="lock" />
              </template>
            </van-field>
          </van-cell-group>

          <div class="form-footer">
            <van-button
              round
              block
              type="primary"
              native-type="submit"
              :loading="loading"
              size="large"
            >
              登 录
            </van-button>
          </div>
        </van-form>
      </div>

      <div class="login-footer">
        <p class="copyright">© 2024 BDMobile OPS. All rights reserved.</p>
      </div>
    </div>
  </div>
</template>

<script setup>
import { ref } from 'vue';
import { useRouter } from 'vue-router';
import { showNotify } from 'vant';
import { useAuthStore } from '@/stores/auth';

const router = useRouter();
const authStore = useAuthStore();
const username = ref('');
const password = ref('');
const loading = ref(false);

const onSubmit = async (values) => {
  try {
    loading.value = true;
    await authStore.login(values.username, values.password);
    showNotify({ type: 'success', message: '登录成功' });
    router.push('/');
  } catch (error) {
    showNotify({ type: 'danger', message: error.message || '登录失败' });
  } finally {
    loading.value = false;
  }
};
</script>

<style>
.login-container {
  min-height: 100vh;
  display: flex;
  align-items: center;
  justify-content: center;
  background: linear-gradient(135deg, var(--background-color) 0%, #e6f3ff 100%);
  padding: 20px;
}

.login-box {
  width: 100%;
  max-width: 420px;
  background: #ffffff;
  border-radius: 16px;
  box-shadow: 0 8px 24px rgba(0, 0, 0, 0.08);
  padding: 32px 24px;
}

.login-header {
  text-align: center;
  margin-bottom: 32px;
}

.logo {
  width: 64px;
  height: 64px;
  margin-bottom: 16px;
}

.title {
  font-size: 24px;
  font-weight: 600;
  color: var(--text-color);
  margin: 0 0 8px;
}

.subtitle {
  font-size: 14px;
  color: var(--text-color-secondary);
  margin: 0;
}

.login-form {
  margin-bottom: 24px;
}

.van-cell-group {
  margin-bottom: 24px;
  --van-cell-group-inset-padding: 16px;
  border-radius: 8px;
  background: var(--background-color);
}

.van-field {
  --van-field-input-text-color: var(--text-color);
}

.van-field__left-icon {
  margin-right: 8px;
  color: var(--text-color-secondary);
}

.form-footer {
  padding: 0 16px;
}

.van-button--primary {
  height: 44px;
  font-size: 16px;
  font-weight: 500;
}

.login-footer {
  text-align: center;
  margin-top: 24px;
}

.copyright {
  font-size: 12px;
  color: var(--text-color-secondary);
  margin: 0;
}

/* 响应式调整 */
@media screen and (max-width: 480px) {
  .login-box {
    padding: 24px 16px;
  }

  .title {
    font-size: 20px;
  }

  .logo {
    width: 48px;
    height: 48px;
  }
}
</style>